package vmcs.controller;

import vmcs.common.Environment;
import vmcs.common.VMCSException;
import vmcs.store.StoreController;
import vmcs.ui.interfacecontrol.ISimulatorPanel;

/**
 * Title:        VMCS
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ISS
 * @author Pang Pingli
 * @version 1.0
 */

public class MainController {
    private SimulationController	simulatorCtrl;
    private MaintenanceController	maintenanceCtrl;
    private StoreController        storeCtrl;
    private TransactionController	transactionCtrl;
    private MachineryController	machineryCtrl;
    public  Environment                 env;

  public MainController() {
  }


  public void initialize()
  throws VMCSException {
    try {
        env = new Environment();
        env.initialize();
        simulatorCtrl = new SimulationController(this);
        
        maintenanceCtrl = new MaintenanceController(this);
        transactionCtrl = new TransactionController(this);
        machineryCtrl = new MachineryController();
        storeCtrl = new StoreController(this);
        storeCtrl.initialize();
        machineryCtrl.initialize(this);
        //------------this is added to correct listener factory pattern problem
        simulatorCtrl.initializeSimulationController(this);
        
    }   catch (VMCSException e){
        throw new VMCSException ("MainController.initialize", e.getMessage());
    }
  }

  public void  start() throws VMCSException {
    try {
        initialize();
        simulatorCtrl.displaySimulatorControlPanel();
        simulatorCtrl.setSimulationActive(false);
    } catch (VMCSException e){
      throw new VMCSException (e);
    }
  }

  public SimulationController  getSimulationController(){
    return simulatorCtrl;
  }

  public ISimulatorPanel getSimulatorControlPanel(){
    return simulatorCtrl.getStartUpPanel();
  }

  public StoreController   getStoreController(){
    return storeCtrl;
  }

   public MaintenanceController   getMaintenanceController(){
    return maintenanceCtrl;
  }
   
   public TransactionController   getTransactionController(){
	    return transactionCtrl;
   }

   public MachineryController   getMachineryController(){
	    return machineryCtrl;
  }

   
  public void closeDown(){

    if (maintenanceCtrl.closeDown() == false)
      return;
   
    try {
        storeCtrl.closeDown();
    } catch (VMCSException e){
      System.out.println("MainController.closeDown"+ e);
    }
    ;
    machineryCtrl.closeDown();
    transactionCtrl.closeDown();
    simulatorCtrl.closeDown();
    
  }
}
